/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fgv.tcc.fgv.imovel.controlle;

import static br.com.fgv.tcc.fgv.imovel.controlle.AbstractCrudBean.logger;
import br.com.fgv.tcc.fgv.imovel.model.Categoria;
import br.com.fgv.tcc.fgv.imovel.model.Programa;
import br.com.fgv.tcc.fgv.imovel.service.CategoriaService;
import br.com.fgv.tcc.fgv.imovel.service.ProgramaService;
import br.com.fgv.tcc.fgv.imovel.service.Service;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import org.apache.log4j.LogManager;
import org.primefaces.model.DualListModel;

/**
 *
 * @author diego
 */
@Named(value = "categoriaM")
@SessionScoped
public class CategoriaMBean extends AbstractCrudBean<Categoria> implements Serializable {

    @EJB
    private CategoriaService ejbFacade;

    @EJB
    private ProgramaService programaService;

    public CategoriaMBean() {
        logger = LogManager.getLogger(Categoria.class.getName());
        selected = new Categoria();

    }

    @Override
    protected Service getServiceManager() {
        return ejbFacade;

    }

    @Override
    public void salvarAtualizar() {
        selected.setListaProgramas(dualListModelProgramas.getTarget());
        super.salvarAtualizar(); //To change body of generated methods, choose Tools | Templates.
    }

    public Categoria getSelected() {
        return selected;
    }

    public void setSelected(Categoria selected) {
        this.selected = selected;

    }

    @Override
    public void novaInstancia() {
        selected = new Categoria();
        selected.setAtiva(Boolean.TRUE);
        iniciaValoresDualList();

    }

    private DualListModel<Programa> dualListModelProgramas;
    private List<Programa> programasNaoIncluso;
    private List<Programa> programasIncluso;

    public DualListModel<Programa> getDualListModelProgramas() {

        List<Programa> todosProgramas = programaService.listarTodos();

        if (selected != null) {
            for (Programa selectedPrograma : selected.getListaProgramas()) {
                todosProgramas.remove(selectedPrograma);
            }
            programasIncluso = selected.getListaProgramas();
            programasNaoIncluso = todosProgramas;
        }
        dualListModelProgramas = new DualListModel<>(programasNaoIncluso, programasIncluso);
       iniciaValoresDualList();
        return dualListModelProgramas;
    }

    public void setDualListModelProgramas(DualListModel<Programa> dualListModelProgramas) {
        this.dualListModelProgramas = dualListModelProgramas;
    }

    private void iniciaValoresDualList() {
        programasIncluso = null;
        programasNaoIncluso = null;
    }
}
